<?php
namespace App\Services\AlipayCustoms;

use App\Services\AlipayCustoms\AlipayCustomRequest;
use App\Services\AlipayCustoms\AlipayCustomResponse;
use Config,DOMDocument;
use App\Services\AlipayCustoms\AlipayCustomsException;
use Log;
class AlipayCustom{


    var $config;

    public function __construct(){

        $this->config = Config::get('alipay-custom');

    }

    public function request($out_trade_no, $trade_no, $pay_price){

        $parameter = array(
            "service" => "alipay.acquire.customs",
            "partner" => trim($this->config['partner']),
            "out_request_no"	=> $out_trade_no,//报关流水号，订单号
            "trade_no"	=> $trade_no,//支付宝交易号
            "merchant_customs_code"	=> trim($this->config['merchant_customs_code']),//商户海关备案编号
            "merchant_customs_name"	=> trim($this->config['merchant_customs_name']),//商户海关备案名称
            "amount"	=> $pay_price,//报关金额
            "customs_place"	=> 'ZHENGZHOU',//海关编号
            "_input_charset"	=> trim(strtolower($this->config['input_charset']))
        );

        Log::info('推送支付宝请求参数'.json_encode($parameter));

        //建立请求
        $alipaySubmit = new AlipayCustomRequest($this->config);

        $html_text = $alipaySubmit->buildRequestHttp($parameter);

        Log::info('推送支付宝响应报文'.$html_text);

        $repsonXmlObj = simplexml_load_string($html_text);

        if($repsonXmlObj->response->alipay->result_code=='SUCCESS'){

            return true;
        }
        else{
            throw new AlipayCustomsException($repsonXmlObj->response->alipay->detail_error_des);
        }

/*echo 'Leen status:'.$repsonXmlObj->Status;
dd($repsonXmlObj);*/
        //echo $html_text;exit();
        //解析XML
        //注意：该功能PHP5环境及以上支持，需开通curl、SSL等PHP配置环境。建议本地调试时使用PHP开发软件
        /*$doc = new DOMDocument();
        $doc->loadXML($html_text);
        //print_r($doc->getElementsByTagName( "alipay" ));exit();
        if( ! empty($doc->getElementsByTagName( "alipay" )->item(0)->nodeValue) ) {
            $alipay = $doc->getElementsByTagName( "alipay" )->item(0)->nodeValue;
            echo $alipay;
        }*/
    }
}